#include "ballclock.h"

linkstack_t *init_linkstack()
{
    linkstack_t *s = NULL;
    s = (linkstack_t *)malloc(sizeof(linkstack_t));
    if (NULL == s)
    {
        printf("malloc failed!\n");
        return NULL;
    }
    memset(s, 0, sizeof(linkstack_t));
    return s;
}

int is_empty_linkstack(linkstack_t *s)
{
    return s->top == NULL ? 1 : 0;
}

void push_linkstack(linkstack_t *s, data_t data)
{
    linknode_t *temp = NULL;
    temp = (linknode_t *)malloc(sizeof(linknode_t));
    if (NULL == temp)
    {
        printf("malloc failed!\n");
        return;
    }
    memset(temp, 0, sizeof(linknode_t));
    temp->data = data;

    // 头插法
    temp->next = s->top;
    s->top = temp;
    s->n++;
    return;
}

data_t pop_linkstack(linkstack_t *s)
{
    linknode_t *temp = NULL;
    data_t data = 0;

    temp = s->top;
    data = temp->data;

    s->top = temp->next;
    free(temp);
    temp = NULL;
    
    s->n--;
    return data;
}

data_t get_top_linkstack(linkstack_t *s)
{
    return s->top->data;
}